package com.dh.manager.util;

import com.dh.core.constant.SysConstant;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class JWTUtils {
    public  String createToken(String openId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("openId", openId);
        JwtBuilder jwtBuilder = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, SysConstant.jwtTokenKey)// 签发算法，秘钥为jwtToken
                .setClaims(claims)//body数据，要唯一，自行设置
                .setIssuedAt(new Date())//设签发时间
                .setExpiration(new Date(System.currentTimeMillis() + SysConstant.jwtTime));// 一天的有效时间
        String token = jwtBuilder.compact();

        return token;
    }

    public  Map<String, Object> checkToken(String token){
            try{
                Jwt parse = Jwts.parser().setSigningKey(SysConstant.jwtTokenKey).parse(token);
                Map<String,Object> map=(Map<String, Object>) parse.getBody();
                System.out.println(parse);
                return map;
            }catch (Exception e){
                System.out.println("解析错误");
                return null;
            }
    }

}
